<!--
  * 产线信息
  *
  * @Author:    lmabbe
  * @Date:      2025-06-26 19:12:15
  * @Copyright  lmabbe
-->
<template>
  <a-modal :destroyOnClose="true" :maskClosable="false" :open="visibleFlag" :title="form.lineId ? '编辑' : '添加'" :width="450" @cancel="onClose">
    <a-form ref="formRef" :label-col="{ span: 5 }" :model="form" :rules="rules">
      <a-input v-model:value="form.lineId" hidden placeholder="产线ID" />
      <a-form-item label="产线名称" name="lineName">
        <a-input v-model:value="form.lineName" placeholder="产线名称" style="width: 100%" />
      </a-form-item>
      <a-form-item label="制程类型" name="processCategory">
        <DictSelect v-model:value="form.processCategory" dict-code="PROCESS_CATEGORY" placeholder="制程类型" width="100%" />
      </a-form-item>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
  import { nextTick, reactive, ref } from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';

  import { productLineInfoApi } from '/@/api/business/product/line/product-line-info-api.js';
  import { smartSentry } from '/@/lib/smart-sentry';
  import DictSelect from '/@/components/support/dict-select/index.vue';

  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList']);

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }
    // 使用字典时把下面这注释修改成自己的字典字段 有多个字典字段就复制多份同理修改 不然打开表单时不显示字典初始值
    // if (form.status && form.status.length > 0) {
    //   form.status = form.status.map((e) => e.valueCode);
    // }
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
    lineId: undefined, //产线ID
    lineName: undefined, //产线名称
    processCategory: undefined, //制程类型
  };

  let form = reactive({ ...formDefault });

  const rules = {
    lineName: [{ required: true, message: '产线名称 必填' }],
    processCategory: [{ required: true, message: '制程类型 必填' }],
  };

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save();
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show();
    try {
      if (form.lineId) {
        await productLineInfoApi.update(form);
      } else {
        await productLineInfoApi.add(form);
      }
      message.success('操作成功');
      emits('reloadList');
      onClose();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  defineExpose({
    show,
  });
</script>
